<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close" :close-on-click-modal="false">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="模型编码" prop="modelKey">
        <el-input
          v-model.trim="form.modelKey"
          autocomplete="off"
          placeholder="不填写模型编码会自动生成"
          maxlength="50"
          :readonly="form.modelId"
        ></el-input>
      </el-form-item>
      <el-form-item label="模型名称" prop="modelName">
        <el-input v-model.trim="form.modelName" autocomplete="off" placeholder="请输入模型名称" maxlength="50"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model.trim="form.description" type="textarea" autocomplete="off" placeholder="请输入描述" maxlength="50"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doAdd } from '@/api/workflow/model'

  export default {
    name: 'ModelerEdit',
    data() {
      return {
        form: {
          modelKey: undefined,
          modelName: undefined,
        },
        rules: {
          modelKey: [{ required: false, trigger: ['blur', 'change'], message: '请输入模型编码' }],
          modelName: [{ required: true, trigger: ['blur', 'change'], message: '请输入模型名称' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '新增'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            await doAdd(this.form).then((res) => this.callbackFun(res))
          } else {
            return false
          }
        })
      },
      callbackFun(res) {
        if (res.success) {
          this.$baseMessage(res.msg, 'success')
          this.$emit('fetch-data')
          this.close()
        } else {
          this.$baseMessage(res.msg, 'error')
        }
      },
    },
  }
</script>
